package cn.luke.basic.services;

import cn.luke.basic.constant.ConstantTemplate;
import cn.luke.basic.entity.DtoTemplate;
import cn.luke.basic.entity.DtoUserTemplate;
import cn.luke.basic.enums.EnumCountType;
import cn.luke.basic.enums.EnumPublicEnableOrDisable;
import cn.luke.basic.utils.UtilsEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import cn.luke.basic.mapper.MapperTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;


@Service
public class ServiceTemplate extends ServiceImpl<MapperTemplate, DtoTemplate> implements BaseService<DtoTemplate> {
    @Resource
    private ServiceTemplateRole serviceTemplateRole;
    @Resource
    private ServiceTemplateCourse serviceTemplateCourse;
    @Resource
    private ServiceTemplateCampus serviceTemplateCampus;
    @Resource
    private ServiceUserTemplate serviceUserTemplate;

    @Resource
    private ServicePayHistory servicePayHistory;

    @Resource
    private MapperTemplate mapperTemplate;

    @Override
    public LambdaQueryWrapper<DtoTemplate> fillParams(Map<String, Object> params) {
        LambdaQueryWrapper<DtoTemplate> qw = loadQw();
        final Object key = params.get("key");
        qw.like(paramNotNull(key), DtoTemplate::getTitle, key)
                .eq(ObjectUtils.isNotEmpty(params.get("status")), DtoTemplate::getStatus, params.get("status"))
                .orderByDesc(DtoTemplate::getSort)
                .orderByDesc(DtoTemplate::getId);

        return qw;
    }

    /**
     * 模板-删除
     */
    @Transactional
    public void deleteById(Integer id) {
        Assert.state(serviceTemplateRole.countByTemplateId(id) == 0, "模板下有规则，不能删除");
        Assert.state(serviceTemplateCourse.countByTemplateId(id) == 0, "模板下有课程，不能删除");
        Assert.state(serviceTemplateCampus.countByTemplateId(id) == 0, "模板下有校区，不能删除");
        Assert.state(serviceUserTemplate.countByTemplateId(id) == 0, "模板有绑定用户，不能删除");
        Assert.state(servicePayHistory.countByTemplateId(id) == 0, "模板下有支付记录，不能删除");
        removeById(id);
    }

    /**
     * 模板-状态修改
     */
    public void updateStatus(Integer id, Integer status) {
        UtilsEnum.existsInEnum(EnumPublicEnableOrDisable.class, status);
        final DtoTemplate dtoTemplate = new DtoTemplate();
        dtoTemplate.setId(id);
        dtoTemplate.setStatus(status);
        updateById(dtoTemplate);
    }

    /**
     * 根据id查询模板数据集合
     */
    public DtoTemplate findById(Integer id) {
        return mapperTemplate.findById(id);
    }



    /**
     * 更新相关数据量
     */
    @Transactional
    public void addCount(Integer id, Integer type) {
        final LambdaQueryWrapper<DtoTemplate> wrapper = loadQw();
        wrapper.eq(DtoTemplate::getId, id);
        final DtoTemplate template = getOne(wrapper);

        if (EnumCountType.PAGE_VIEW.getCode().equals(type)) {
            template.setPageView(template.getPageView() + ConstantTemplate.COUNT);
        } else if (EnumCountType.SHARE_COUNT.getCode().equals(type)) {
            template.setShareCount(template.getShareCount() + ConstantTemplate.COUNT);
        } else if (EnumCountType.PARTICIPATION.getCode().equals(type)) {
            template.setParticipation(template.getParticipation() + ConstantTemplate.COUNT);
        }
        updateById(template);
    }
}